import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:jetour_line/base_widget/base_scaffold.dart';
import 'package:jetour_line/base_widget/loading_widget.dart';
import 'package:jetour_line/base_widget/my_refresh_view.dart';
import 'package:jetour_line/pages/baseWidget/common_item_widget.dart';
import 'package:jetour_line/pages/baseWidget/title_widget.dart';
import 'package:jetour_line/pages/mine/trajectoryDetail/trajectory_detail_page.dart';
import 'package:jetour_line/utils/extension_int.dart';

import '../../models/guide_list_model.dart';
import '../../my_colors.dart';
import '../../network/aha_http.dart';
import '../../network/jetour_http.dart';
import '../../utils/page_push_utils.dart';
import '../baseWidget/line_item_widget.dart';
import "package:jetour_line/models/line_list_model.dart" as line_list;
import '../../model/trajectory_lsit_model.dart' as tra_list;
class MineCollectPageScreen extends StatefulWidget {
  const MineCollectPageScreen({Key? key}) : super(key: key);

  @override
  State<MineCollectPageScreen> createState() => _MineCollectPageScreenState();
}

class _MineCollectPageScreenState extends State<MineCollectPageScreen> {
  int commonPage = 1;
  int traPage = 1;
  int linePage = 1;
  int selIndex = 0;
  final MyRefreshViewController _controller = MyRefreshViewController();
  List<ListElement>? commonList;

  List<line_list.ListElement>? lineList;
  List<tra_list.ListElement>? traList;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    mineList();
  }

  @override
  Widget build(BuildContext context) {
    bool showLoading = false;
    int type = 0;
    if (selIndex == 0) {
      if (commonList == null || commonList!.isEmpty) {
        showLoading = true;
        if (commonList != null && commonList!.isEmpty) {
          type = 1;
        }
      }
    }else if (selIndex == 1)  {
      if (lineList == null || lineList!.isEmpty) {
        showLoading = true;
        if (lineList != null && lineList!.isEmpty) {
          type = 1;
        }
      }
    }else if (selIndex == 2)  {
      if (traList == null || traList!.isEmpty) {
        showLoading = true;
        if (traList != null && traList!.isEmpty) {
          type = 1;
        }
      }
    }
    return BaseScaffold(
        title: "我的收藏",
        body: Column(
          children: [
            Container(
              color: Colors.red,
              width: double.infinity,
              height: 100.px,
              child: TitleWidget(
                const ['共创路书', '精选路书', '轨迹路书'],
                selectIndexCallBack: (index) {
                  selIndex = index;
                  mineList();
                  setState(() {});
                },
              ),
            ),
            Expanded(
                child: Padding(
              padding: EdgeInsets.all(20.px),
              child: MyRefreshView(
                refreshCallBack: (bool isUp) {
                  if (isUp) {
                    if (selIndex == 0) {
                      commonPage == 1;
                    } else  if (selIndex == 1) {
                      linePage = 1;
                    }else  if (selIndex == 2) {
                      traPage = 1;
                    }
                  } else if(selIndex == 1){
                    if (selIndex == 0) {
                      commonPage++;
                    }else  if (selIndex == 1) {
                      linePage ++;
                    }else  if (selIndex == 2) {
                      traPage ++;
                    }
                  }
                  mineList();
                },
                controller: _controller,
                child: showLoading
                    ? Container(
                        width: double.infinity,
                        height: 1000.px,
                        child: LoadingWidget(
                          type: type,
                        ),
                      )
                    : selIndex == 0
                        ? ListView.builder(
                            itemCount: commonList!.length,
                            itemBuilder: (context, index) {
                              return CommonItemWidget(
                                  comment: commonList![index],collectCallBack: (bool isC){
                                    if(!isC){//取消收藏
                                      commonList!.removeAt(index);
                                      if(commonList!.isEmpty){
                                        mineList();
                                      }
                                      setState(() {

                                      });
                                    }
                              },);
                            },
                          )
                        : selIndex == 1
                            ? ListView.builder(
                                itemCount:
                                    lineList == null ? 0 : lineList!.length,
                                itemBuilder: (context, index) {
                                  return LineItemWidget(line: lineList![index],collectCallBack:(bool isC){
                                    if(!isC){//取消收藏
                                      lineList!.removeAt(index);
                                      if(lineList!.isEmpty){
                                        mineList();
                                      }
                                      setState(() {

                                      });
                                    }
                                  } ,);
                                },
                              )
                            : ListView.builder(
                                itemCount:
                                traList == null ? 0 : traList!.length,
                                itemBuilder: (context, index) {
                                  return itemWidget(index);
                                },
                              ),
              ),
            )),
          ],
        ));
  }

  void mineList() {
    Map<String, dynamic> params = Map();
    if (selIndex == 0) {
      params['page'] = commonPage;
      JetourHttp()
          .get(JetourRouter.GuideMyCollect, params: params)
          .then((value) {
        commonList ??= [];
        UserGuideListModel data = UserGuideListModel.fromJson(value);
        _controller.refreshEnd(page: commonPage, maxPage: data.meta.maxPage);
        if (commonPage == 1) {
          commonList!.clear();
        }
        commonList!.addAll(data.list);
        setState(() {});
      });
    } else {
      if(selIndex == 1){
        params['type'] = 1;
        params['page'] = linePage;
      }else{
        params['type'] = 12;
        params['page'] = traPage;
      }
      AhaHttp().get(AhaRouter.MyCollectList, params: params).then((value) {
        if(selIndex == 1){
          lineList ??= [];
          line_list.LineListModel data = line_list.LineListModel.fromJson(value);
          _controller.refreshEnd(page: commonPage, maxPage: data.meta.maxPage);
          if (linePage == 1) {
            lineList!.clear();
          }
          lineList!.addAll(data.list);
        }else{
          traList ??= [];
          tra_list.TrajectoryListModel data = tra_list.TrajectoryListModel.fromJson(value);
          _controller.refreshEnd(page: commonPage, maxPage: data.meta.maxPage);
          if (linePage == 1) {
            traList!.clear();
          }
          traList!.addAll(data.list);
        }
        setState(() {});
      });
    }
  }


  Widget itemWidget(int index) {
    String caption = traList![index].caption;
    String time = '于${traList![index].time.year}-${traList![index].time.month}-${traList![index].time.day}创建';
    String dis = '总里程${traList![index].distance}';


    return Padding(
      padding: EdgeInsets.only(
        left: 10.px,
        top: 20.px,
        right: 10.px,
      ),
      child: ClipRRect(
        borderRadius: BorderRadius.all(Radius.circular(16.px)),
        child: GestureDetector(
          onTap: () {
            PagePushUtils.push(context, TrajectoryDetailScreen(id: traList![index].id));
          },
          child: Container(
            color: MyColors.white,
            width: double.infinity,
            child: Padding(
              padding: EdgeInsets.all(
                24.px,
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    caption,
                    style: TextStyle(
                        fontSize: 30.px, color: MyColors.color333333),
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis,
                  ),
                  SizedBox(height: 15.px,),
                  Row(children: [
                    Text(
                      time,
                      style: TextStyle(
                          fontSize: 28.px, color: MyColors.color666666),
                      maxLines: 2,
                      overflow: TextOverflow.ellipsis,
                    ),
                    SizedBox(width: 10.px,),
                    Expanded(child: Text(
                      dis,
                      style: TextStyle(
                          fontSize: 26.px, color: MyColors.color999999),
                      maxLines: 2,
                      overflow: TextOverflow.ellipsis,
                    ),),

                  ],),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }

}
